""" Fully Homomorphic Encryption root workspace """

workspace(name = "com_google_fully_homomorphic_encryption")

load(
    "@bazel_tools//tools/build_defs/repo:git.bzl",
    "new_git_repository",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

# provides the `license` rule, which is required because gentbl_rule implicitly
# depends upon the target '//:license'.
http_archive(
    name = "rules_license",
    sha256 = "6157e1e68378532d0241ecd15d3c45f6e5cfd98fc10846045509fb2a7cc9e381",
    urls = [
        "https://github.com/bazelbuild/rules_license/releases/download/0.0.4/rules_license-0.0.4.tar.gz",
    ],
)

http_archive(
    name = "bazel_skylib",
    sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz",
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz",
    ],
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
    name = "rules_foreign_cc",
    sha256 = "bcd0c5f46a49b85b384906daae41d277b3dc0ff27c7c752cc51e43048a58ec83",
    strip_prefix = "rules_foreign_cc-0.7.1",
    url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.7.1.tar.gz",
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()

http_archive(
    name = "com_nlohmann_json",
    build_file = "//patches:nlohmann_json.BUILD",
    sha256 = "d69f9deb6a75e2580465c6c4c5111b89c4dc2fa94e3a85fcd2ffcd9a143d9273",
    strip_prefix = "json-3.11.2",
    url = "https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.tar.gz",
)

# Install TFHE
http_archive(
    name = "tfhe",
    build_file = "//patches:tfhe.BUILD",
    sha256 = "7ad88b70b389bfdb871488a90372b0cecd9ba731183ba02c3cd0ce86c9adcc93",
    strip_prefix = "tfhe-a085efe91314f994285fcb06ab8bdae3d55e4505",
    url = "https://github.com/tfhe/tfhe/archive/a085efe91314f994285fcb06ab8bdae3d55e4505.tar.gz",
)

# Install OpenFHE
# git_repository is required because the git project uses submodules,
# (see .gitmodules in the source project), and hence we have to rely
# on init_submodules to get proper behavior
git_repository(
    name = "openfhe",
    build_file = "//bazel:openfhe.BUILD",
    # Currently v1.1.4, 2024-03-08
    commit = "94fd76a1d965cfde13f2a540d78ce64146fc2700",
    patches = ["//bazel:add_config_core_openfhe.patch"],
    remote = "https://github.com/openfheorg/openfhe-development.git",
)

git_repository(
    name = "cereal",
    build_file = "//bazel:cereal.BUILD",
    commit = "ebef1e929807629befafbb2918ea1a08c7194554",
    remote = "https://github.com/USCiLab/cereal.git",
)

git_repository(
    name = "rapidjson",
    build_file = "//bazel:rapidjson.BUILD",
    commit = "9bd618f545ab647e2c3bcbf2f1d87423d6edf800",
    remote = "https://github.com/Tencent/rapidjson.git",
)


# Install XLS and its transitive dependencies.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_python",
    sha256 = "ca77768989a7f311186a29747e3e95c936a41dffac779aff6b443db22290d913",
    strip_prefix = "rules_python-0.36.0",
    url = "https://github.com/bazelbuild/rules_python/releases/download/0.36.0/rules_python-0.36.0.tar.gz",
)
### Protobuf setup section
# Note that protobuf setup is very complicated and needs its own carve-out in WORKSPACE.
PROTOBUF_VERSION = "30.1"
PROTOBUF_HASH = "008a11cc56f9b96679b4c285fd05f46d317d685be3ab524b2a310be0fbad987e"
http_archive(
    name = "com_google_protobuf",
    sha256 = PROTOBUF_HASH,
    strip_prefix = "protobuf-" + PROTOBUF_VERSION,
    urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v{0}/protobuf-{0}.tar.gz".format(PROTOBUF_VERSION)],
)

# note that the following line is what is minimally required from protobuf for the java rules
# consider using the protobuf_deps() public API from @com_google_protobuf//:protobuf_deps.bzl
load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features")  # buildifier: disable=bzl-visibility

proto_bazel_features(name = "proto_bazel_features")

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()
http_archive(
    name = "com_google_xls",
    sha256 = "d671f2f6622517f04b42e7ab8f6bc42b5ecc37f0ae25473f0335d23f829f7a9f",
    strip_prefix = "xls-1f56aafdb5fd0a4310f7239cf54f1e5815d319f7",
    url = "https://github.com/google/xls/archive/1f56aafdb5fd0a4310f7239cf54f1e5815d319f7.tar.gz",
    patch_args = ["-p1"],
    patches = ["//patches:xls.diff", "//patches:xls_visibility.diff", "//patches:xls_repo.diff"],
    patch_cmds = [
        "sed -i s/3.11/3.12/g WORKSPACE",
    ],
)

load("@com_google_xls//dependency_support:load_external.bzl", "load_external_repositories")

load_external_repositories()

load(
  "@rules_python//python:repositories.bzl",
  "py_repositories",
  "python_register_toolchains",
)

# Must be called before using anything from rules_python.
# https://github.com/bazelbuild/rules_python/issues/1560#issuecomment-1815118394
py_repositories()

python_register_toolchains(
    name = "project_python",
    python_version = "3.11",
)

load("@com_google_xls//dependency_support:initialize_external.bzl", "initialize_external_repositories")

initialize_external_repositories()

load("@xls_pip_deps//:requirements.bzl", xls_pip_install_deps = "install_deps")

xls_pip_install_deps()

# Install ABC
http_archive(
    name = "abc",
    build_file = "//patches:abc.BUILD",
    sha256 = "7fa5a448a4309fb4d6cf856c3fe4cc4be46b09dd552a05d5cfacd75f8d9504ad",
    strip_prefix = "abc-eb44a80bf2eb8723231e72bb095c97d1e4834d56",
    urls = [
        "https://github.com/berkeley-abc/abc/archive/eb44a80bf2eb8723231e72bb095c97d1e4834d56.zip",
    ],
)

# Install rules_rust for rust codegen
http_archive(
    name = "rules_rust",
    sha256 = "4a9cb4fda6ccd5b5ec393b2e944822a62e050c7c06f1ea41607f14c4fdec57a2",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.25.1/rules_rust-v0.25.1.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains(
    edition = "2021",
    versions = [
        # see https://github.com/bazelbuild/rules_rust/blob/main/util/fetch_shas_VERSIONS.txt
        "1.70.0",
    ],
)

# Rust dependencies ("Cargo free" setup)
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "render_config")

crates_repository(
    name = "crate_index",
    cargo_lockfile = "//:Cargo.lock",
    lockfile = "//:Cargo.Bazel.lock",
    packages = {
        "tfhe": crate.spec(
            features = [
                "boolean",
                "shortint",
                "x86_64-unix",
            ],
            version = "0.2.4",
        ),
        "rayon": crate.spec(
            version = "1.7.0",
        ),
    },
    render_config = render_config(
        default_package_name = "",
    ),
)

load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

# @xls_pip_deps installs rules_python and uses the system python, and for
# whatever reason that means we can't use a hermetic python here. If you try,
# you will see it fail to load numpy C-extensions.
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
py_repositories()
load("@rules_python//python:pip.bzl", "pip_parse")
pip_parse(
    name = "transpiler_pip_deps",
    requirements_lock = "//:requirements.txt",
)
load("@transpiler_pip_deps//:requirements.bzl", "install_deps")
install_deps()

# install dependencies for yosys/ABC circuit optimizers
http_archive(
    name = "rules_hdl",
    # Commit 2024-02-14, after merging our patch to fix MacOS builds
    strip_prefix = "bazel_rules_hdl-9f292fca574e6406a69eeb91daad18a4c8ac2252",
    url = "https://github.com/hdl/bazel_rules_hdl/archive/9f292fca574e6406a69eeb91daad18a4c8ac2252.tar.gz",
)

load("@rules_hdl//dependency_support/at_clifford_yosys:at_clifford_yosys.bzl", "at_clifford_yosys")
load("@rules_hdl//dependency_support/edu_berkeley_abc:edu_berkeley_abc.bzl", "edu_berkeley_abc")
load("@rules_hdl//dependency_support/net_invisible_island_ncurses:net_invisible_island_ncurses.bzl", "net_invisible_island_ncurses")
load("@rules_hdl//dependency_support/net_zlib:net_zlib.bzl", "net_zlib")
load("@rules_hdl//dependency_support/org_gnu_gnulib:org_gnu_gnulib.bzl", "org_gnu_gnulib")
load("@rules_hdl//dependency_support/org_gnu_readline:org_gnu_readline.bzl", "org_gnu_readline")
load("@rules_hdl//dependency_support/org_sourceware_libffi:org_sourceware_libffi.bzl", "org_sourceware_libffi")
load("@rules_hdl//dependency_support/tk_tcl:tk_tcl.bzl", "tk_tcl")
load("@rules_hdl//dependency_support/org_fftw:org_fftw.bzl", "org_fftw")

net_invisible_island_ncurses()

org_gnu_readline()

edu_berkeley_abc()

tk_tcl()

net_zlib()

org_gnu_gnulib()

org_sourceware_libffi()

at_clifford_yosys()

org_fftw()